from apps.clsbb.models import *
from apps.pam.models import Pam_business_opportunity_Records, Pam_business_opportunityCheck, Pam_Progress_log, \
    Pam_Contract_tobesignedCheck, Pam_Contract_tobesigned_Records, Pam_Contract_tobesigned_Progress_log, \
    Pam_contract_managementCheck, Pam_contract_management_Records, Pam_framecontract_orderCheck, \
    Pam_framecontract_order_Records, Pam_framecontract_order_whole_Records, Pam_framecontract_order_wholeCheck, \
    Pam_singlecontract_order_wholeCheck, Pam_singlecontract_order_whole_Records, Pam_supplier_management_Records
from apps.system.models import Region
from extensions.serializers import *


class Pam_business_opportunity_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    sphere_name = CharField(source='sphere.name',  read_only=True, label='圈层')
    attribute_of_first_party_name = CharField(source='attribute_of_first_party.name',  read_only=True, label='甲方属性')
    field_business_attribute_name = CharField(source='field_business_attribute.name',  read_only=True, label='业务属性之领域')
    classification_business_attribute_name = CharField(source='classification_business_attribute.name',  read_only=True, label='业务属性之分类')
    success_rate_estimate_name = CharField(source='success_rate_estimate.name',  read_only=True, label='成功率')
    state_name = CharField(source='state.name',  read_only=True, label='状态')
    result_name = CharField(source='result.name',  read_only=True, label='结果')

    class Pam_business_opportunityChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_business_opportunityCheck
            fields = ['id', 'pam_business_opportunity','check_user','check_user_name','opinion','is_agree','add_time']

    pam_business_opportunity_checks_items = Pam_business_opportunityChecksSerializer(
        source='Pam_business_opportunityCheck', many=True, read_only=True, label='审核记录')

    class Pam_Progress_logsSerializer(BaseSerializer):
        filled_by_name = CharField(source='filled_by.name', read_only=True, label='填报人')
        class Meta:
            model = Pam_Progress_log
            fields = ['id', 'filled_by_name','progress','filled_by','add_time']

    Pam_Progress_logs_items = Pam_Progress_logsSerializer(
        source='Pam_business_opportunity_Progress_log', many=True, read_only=True, label='进展情况')

    class Meta:
        model = Pam_business_opportunity_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                            'pam_business_opportunity_checks_items','sphere_name',
                            'Pam_Progress_logs_items']
        fields = ['company',
                  'number','region','name',
                  'is_frame',
                  'attribute_of_first_party_name',
                  'field_business_attribute_name',
                  'classification_business_attribute_name',
                  'success_rate_estimate_name',
                  'state_name',
                  'result_name',
                  'name_of_first_party',
                  'collection_date',
                  'project_content',
                  'construction_area',
                  'sphere',
                  'attribute_of_first_party',
                  'field_business_attribute',
                  'classification_business_attribute',
                  'budget',
                  'tax_rate',
                  'success_rate_estimate',
                  'state',
                  'result',
                  'estimated_signing_date',
                  'person_in_charge',
                  'filled_by',
                  'update_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  'update_time',
                  'del_flag',
                  'remarks',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Pam_Contract_tobesigned_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    sphere_name = CharField(source='sphere.name',  read_only=True, label='圈层')
    attribute_of_first_party_name = CharField(source='attribute_of_first_party.name',  read_only=True, label='甲方属性')
    field_business_attribute_name = CharField(source='field_business_attribute.name',  read_only=True, label='业务属性之领域')
    classification_business_attribute_name = CharField(source='classification_business_attribute.name',  read_only=True, label='业务属性之分类')
    project_source_name = CharField(source='project_source.name',  read_only=True, label='项目来源')
    result_name = CharField(source='result.name',  read_only=True, label='结果')

    class Pam_Contract_tobesignedChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_Contract_tobesignedCheck
            fields = ['id', 'pam_Contract_tobesigned','check_user','check_user_name','opinion','is_agree','add_time']

    pam_Contract_tobesigned_checks_items = Pam_Contract_tobesignedChecksSerializer(
        source='Pam_Contract_tobesignedCheck', many=True, read_only=True, label='审核记录')

    class Pam_Progress_logsSerializer(BaseSerializer):
        filled_by_name = CharField(source='filled_by.name', read_only=True, label='填报人')
        class Meta:
            model = Pam_Contract_tobesigned_Progress_log
            fields = ['id', 'filled_by_name','progress','filled_by','add_time']

    Pam_Progress_logs_items = Pam_Progress_logsSerializer(
        source='Pam_Contract_tobesigned_Progress_log', many=True, read_only=True, label='进展情况')

    class Meta:
        model = Pam_Contract_tobesigned_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                            'pam_Contract_tobesigned_checks_items','sphere_name',
                            'Pam_Progress_logs_items']
        fields = ['company',
                  'number','region','name',
                  'is_frame',
                  'attribute_of_first_party_name',
                  'field_business_attribute_name',
                  'classification_business_attribute_name',
                  'project_source_name',
                  'result_name',
                  'name_of_first_party',
                  'project_content',
                  'construction_area',
                  'sphere',
                  'attribute_of_first_party',
                  'field_business_attribute',
                  'classification_business_attribute',
                  'budget',
                  'tax_rate',
                  'project_source',
                  'result',
                  'estimated_signing_date',
                  'person_in_charge',
                  'filled_by',
                  'update_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  'update_time',
                  'del_flag',
                  'remarks',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Pam_contract_management_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    sphere_name = CharField(source='sphere.name',  read_only=True, label='圈层')
    attribute_of_first_party_name = CharField(source='attribute_of_first_party.name',  read_only=True, label='甲方属性')
    field_business_attribute_name = CharField(source='field_business_attribute.name',  read_only=True, label='业务属性之领域')
    classification_business_attribute_name = CharField(source='classification_business_attribute.name',  read_only=True, label='业务属性之分类')
    project_source_name = CharField(source='project_source.name',  read_only=True, label='项目来源')
    result_name = CharField(source='result.name',  read_only=True, label='结果')

    class Pam_contract_managementChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_contract_managementCheck
            fields = ['id', 'pam_contract_management','check_user','check_user_name','opinion','is_agree','add_time']

    pam_contract_management_checks_items = Pam_contract_managementChecksSerializer(
        source='Pam_contract_managementCheck', many=True, read_only=True, label='审核记录')


    class Meta:
        model = Pam_contract_management_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                            'pam_contract_management_checks_items','sphere_name',
                            ]
        fields = ['company',
                  'number','region','name',
                  'is_frame',
                  'attribute_of_first_party_name',
                  'field_business_attribute_name',
                  'classification_business_attribute_name',
                  'project_source_name',
                  'result_name',
                  'erp_number',
                  'signing_date',
                  'expiry_date',
                  'name_of_first_party',
                  'sphere',
                  'attribute_of_first_party',
                  'field_business_attribute',
                  'classification_business_attribute',
                  'contract_amount',
                  'estimated_cash_amount',
                  'estimated_cash_date',
                  'tax_rate',
                  'project_source',
                  'person_liable',
                  'filled_by',
                  'update_by',
                  'erp_contract_seal_date',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  'update_time',
                  'del_flag',
                  'remarks',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Pam_framecontract_order_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    project_type_name = CharField(source='project_type.name',  read_only=True, label='项目类型')
    pam_contract_management_name = CharField(source='pam_contract_management.name',  read_only=True, label='项目类型')

    class Pam_contract_managementsSerializer(BaseSerializer):
        sphere_name = CharField(source='sphere.name', read_only=True, label='圈层')
        attribute_of_first_party_name = CharField(source='attribute_of_first_party.name', read_only=True, label='甲方属性')
        field_business_attribute_name = CharField(source='field_business_attribute.name', read_only=True,
                                                  label='业务属性之领域')
        classification_business_attribute_name = CharField(source='classification_business_attribute.name',
                                                           read_only=True, label='业务属性之分类')
        project_source_name = CharField(source='project_source.name', read_only=True, label='项目来源')
        class Meta:
            model = Pam_contract_management_Records
            read_only_fields = ['sphere_name',
            'attribute_of_first_party_name',
            'field_business_attribute_name',
            'classification_business_attribute_name',
            'project_source_name',]
            fields = '__all__'

    pam_contract_managements_items = Pam_contract_managementsSerializer(
        source='pam_contract_management', many=False, read_only=True, label='合同详细')

    class Pam_framecontract_orderChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_framecontract_order_wholeCheck
            read_only_fields = ['check_user_name',]
            fields = '__all__'

    pam_framecontract_order_checks_items = Pam_framecontract_orderChecksSerializer(
        source='pam_framecontract_order_whole', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Pam_framecontract_order_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['company_name',
                            'pam_framecontract_order_checks_items',
                            'project_type_name',
                            'pam_contract_managements_items',
                            'pam_contract_management_name',
                            'filled_by_name',
                            'next_checker_name',
                            ]
        fields = '__all__'



    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Pam_framecontract_order_whole_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='pam_framecontract_order.company.name', read_only=True, label='填报人')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    construction_organization_mode_name = CharField(source='construction_organization_mode.name',  read_only=True, label='施工组织方式')
    labor_subcontracting_mode_name = CharField(source='labor_subcontracting_mode.name',  read_only=True, label='劳务分包方式')
    order_execution_name = CharField(source='order_execution.name',  read_only=True, label='订单执行情况')

    class Pam_framecontract_ordersSerializer(BaseSerializer):
        class Meta:
            model = Pam_framecontract_order_Records
            fields = '__all__'

    pam_framecontract_orders_items = Pam_framecontract_ordersSerializer(
        source='pam_framecontract_order', many=False, read_only=True, label='订单详细')

    class Pam_framecontract_order_wholeChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_framecontract_order_wholeCheck
            read_only_fields = ['check_user_name',]
            fields = '__all__'

    pam_framecontract_order_whole_checks_items = Pam_framecontract_order_wholeChecksSerializer(
        source='pam_framecontract_order_whole', many=True, read_only=True, label='审核记录')

    class Pam_contract_managementsSerializer(BaseSerializer):
        sphere_name = CharField(source='sphere.name', read_only=True, label='圈层')
        company_name = CharField(source='company.name', read_only=True, label='圈层')
        attribute_of_first_party_name = CharField(source='attribute_of_first_party.name', read_only=True,
                                                  label='甲方属性')
        field_business_attribute_name = CharField(source='field_business_attribute.name', read_only=True,
                                                  label='业务属性之领域')
        classification_business_attribute_name = CharField(source='classification_business_attribute.name',
                                                           read_only=True, label='业务属性之分类')
        project_source_name = CharField(source='project_source.name', read_only=True, label='项目来源')

        class Meta:
            model = Pam_contract_management_Records
            read_only_fields = ['sphere_name',
                                'company_name',
                                'attribute_of_first_party_name',
                                'field_business_attribute_name',
                                'classification_business_attribute_name',
                                'project_source_name', ]
            fields = '__all__'

    pam_contract_managements_items = Pam_contract_managementsSerializer(
        source='pam_framecontract_order.pam_contract_management', many=False, read_only=True, label='合同详细')

    class Meta:
        model = Pam_framecontract_order_whole_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = [
                            'company_name',
                            'pam_framecontract_order_whole_checks_items',
                            'pam_framecontract_orders_items',
                            'pam_contract_managements_items',
                            'filled_by_name',
                            'next_checker_name',
                            ]
        fields = '__all__'



    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Pam_singlecontract_order_whole_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='pam_contract_management.company.name', read_only=True, label='填报人')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')
    construction_organization_mode_name = CharField(source='construction_organization_mode.name',  read_only=True, label='施工组织方式')
    labor_subcontracting_mode_name = CharField(source='labor_subcontracting_mode.name',  read_only=True, label='劳务分包方式')
    order_execution_name = CharField(source='order_execution.name',  read_only=True, label='订单执行情况')

    class Pam_singlecontract_order_wholeChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = Pam_singlecontract_order_wholeCheck
            read_only_fields = ['check_user_name',]
            fields = '__all__'

    pam_singlecontract_order_whole_checks_items = Pam_singlecontract_order_wholeChecksSerializer(
        source='pam_singlecontract_order_whole', many=True, read_only=True, label='审核记录')

    class Pam_contract_managementsSerializer(BaseSerializer):
        sphere_name = CharField(source='sphere.name', read_only=True, label='圈层')
        company_name = CharField(source='company.name', read_only=True, label='圈层')
        attribute_of_first_party_name = CharField(source='attribute_of_first_party.name', read_only=True,
                                                  label='甲方属性')
        field_business_attribute_name = CharField(source='field_business_attribute.name', read_only=True,
                                                  label='业务属性之领域')
        classification_business_attribute_name = CharField(source='classification_business_attribute.name',
                                                           read_only=True, label='业务属性之分类')
        project_source_name = CharField(source='project_source.name', read_only=True, label='项目来源')

        class Meta:
            model = Pam_contract_management_Records
            read_only_fields = ['sphere_name',
                                'company_name',
                                'attribute_of_first_party_name',
                                'field_business_attribute_name',
                                'classification_business_attribute_name',
                                'project_source_name', ]
            fields = '__all__'

    pam_contract_managements_items = Pam_contract_managementsSerializer(
        source='pam_contract_management', many=False, read_only=True, label='合同详细')

    class Meta:
        model = Pam_singlecontract_order_whole_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = [
                            'company_name',
                            'pam_singlecontract_order_whole_checks_items',
                            'pam_contract_managements_items',
                            'filled_by_name',
                            'next_checker_name',
                            ]
        fields = '__all__'



    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)

    # def to_representation(self, instance):
    #     # pam_contract_managements_items = self.pam_contract_managements_items
    #     print(instance.pam_contract_management.contract_amount)
    #     # pam_contract_management_values = Pam_contract_management_Records.objects.filter(id=instance).aggregate(Sum('contract_amount'))
    #     # return pam_contract_management_values['contract_amount__sum'] or 0
    #     return instance.pam_contract_management.contract_amount


class Clsbbalk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    product_name = CharField(source='product.name',  read_only=True, label='产品')
    industry_name = CharField(source='industry.name',  read_only=True, label='行业')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_case_item = Clsbb_Common_PdfsSerializer(source='attachments_case', read_only=True, label='案例文档附件')
    acceptance_report_item = Clsbb_Common_PdfsSerializer(source='acceptance_report', read_only=True, label='案例文档附件')
    case_contract_item = Clsbb_Common_PdfsSerializer(source='case_contract', read_only=True, label='案例文档附件')
    notification_announcement_item = Clsbb_Common_PdfsSerializer(source='notification_announcement', read_only=True, label='案例文档附件')
    bidding_documents_item = Clsbb_Common_PdfsSerializer(source='bidding_documents', read_only=True, label='案例文档附件')
    class ClsbbalkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbalkCheck
            fields = ['id', 'clsbbalk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbalkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbalk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'product_name',
                  'industry_name',
                  'attachments_case_item',
                  'acceptance_report_item',
                  'case_contract_item',
                  'notification_announcement_item',
                  'bidding_documents_item',

                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','keywords','name',
                  'describe',
                  'contact',
                  'contact_phone',
                  'name_partne',
                  'contact_partne',
                  'contact_phone_partne',
                  'maintainer',
                  'maintainer_phone',
                  'remarks',
                  'customer_name',
                  'industry',
                  'product',
                  'project_scale',
                  'bidding_or_not',
                  'signatory',
                  'bid_winning_amount',
                  'attachments_case',
                  'acceptance_report',
                  'case_contract',
                  'notification_announcement',
                  'bidding_documents',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Clsbbtbk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    product_name = CharField(source='product.name',  read_only=True, label='产品')
    industry_name = CharField(source='industry.name',  read_only=True, label='行业')
    template_type_name = CharField(source='template_type.name',  read_only=True, label='行业')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_data_item = Clsbb_Common_PdfsSerializer(source='attachments_data', read_only=True, label='资料附件')
    class ClsbbtbkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbtbkCheck
            fields = ['id', 'clsbbtbk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbtbkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbtbk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'product_name',
                  'industry_name',
                  'template_type_name',
                  'attachments_data_item',

                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','keywords','name',
                  'describe',
                  'contact',
                  'contact_phone',
                  'contact_email',
                  'maintainer',
                  'maintainer_phone',
                  'maintainer_email',
                  'remarks',
                  'template_type',
                  'industry',
                  'product',
                  'attachments_data',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Clsbbcpk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_plan_item = Clsbb_Common_PdfsSerializer(source='attachments_plan', read_only=True, label='产品方案')
    attachments_other_item = Clsbb_Common_PdfsSerializer(source='attachments_other', read_only=True, label='其它附件')
    class ClsbbcpkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbcpkCheck
            fields = ['id', 'clsbbcpk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbcpkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbcpk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'attachments_plan_item',
                  'attachments_other_item',

                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','name',
                  'model',
                  'functions',
                  'cost_price',
                  'settlement_price',
                  'manufactor',
                  'delete_flag',
                  'contact',
                  'contact_phone',
                  'maintainer',
                  'maintainer_phone',
                  'remarks',
                  'attachments_plan',
                  'attachments_other',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Clsbbhzhbk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_qualification_item = Clsbb_Common_PdfsSerializer(source='attachments_qualification', read_only=True, label='产品方案')
    class ClsbbhzhbkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbhzhbkCheck
            fields = ['id', 'clsbbhzhbk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbhzhbkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbhzhbk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'attachments_qualification_item',
                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','name',
                  'describe',
                  'business_scope',
                  'business_qualification',
                  'agreement_is_signed',
                  'framework_agreement_is_signed',
                  'channel_purchased_is_needs',
                  'Introduction_method',
                  'contact',
                  'contact_phone',
                  'maintainer',
                  'maintainer_phone',
                  'attachments_qualification',
                  'remarks',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Clsbbgszzk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_company_qualification_item = Clsbb_Common_PdfsSerializer(source='attachments_company_qualification', read_only=True, label='公司资质附件')
    class ClsbbgszzkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbgszzkCheck
            fields = ['id', 'clsbbgszzk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbgszzkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbgszzk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'attachments_company_qualification_item',
                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','name',
                  'issued_by',
                  'issuing_date',
                  'expiration_date',
                  'renewal_date',
                  'qualification_status',
                  'qualification_classification',
                  'qualification_grade',
                  'contact',
                  'contact_phone',
                  'maintainer',
                  'maintainer_phone',
                  'attachments_company_qualification',
                  'remarks',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)



class Clsbbryzzk_RecordsSerializer(BaseSerializer):
    company_name = CharField(source='company.name',  read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    next_checker_name = CharField(source='next_checker.name',  read_only=True, label='下一步审核人')

    class Clsbb_Common_PdfsSerializer(BaseSerializer):
        class Meta:
            model = Clsbb_Common_Pdfs
            fields = ['id', 'file','name','type']

    attachments_personnel_qualification_item = Clsbb_Common_PdfsSerializer(source='attachments_personnel_qualification', read_only=True, label='人员资质附件')
    class ClsbbryzzkChecksSerializer(BaseSerializer):
        check_user_name = CharField(source='check_user.name', read_only=True, label='审核人')
        class Meta:
            model = ClsbbryzzkCheck
            fields = ['id', 'clsbbryzzk','check_user','check_user_name','opinion','is_agree','add_time']

    clsbb_checks_items = ClsbbryzzkChecksSerializer(
        source='check_records', many=True, read_only=True, label='审核记录')

    class Meta:
        model = Clsbbryzzk_Records
        # read_only_fields = ['id','clsbbfak_pdf_item','clsbbfak_checks_items','downloads_logs_items','views_logs_items','downloads', 'views']
        read_only_fields = ['id', 'company_name',
                  'attachments_personnel_qualification_item',
                'clsbb_checks_items','downloads', 'views']
        fields = ['company',
                  'number','name',
                  'full_name',
                  'certification_discipline',
                  'certificate_number',
                  'issued_by',
                  'issuing_date',
                  'expiration_date',
                  'renewal_date',
                  'qualification_status',
                  'maintainer',
                  'maintainer_phone',
                  'professional_qualification_level',
                  'attachments_personnel_qualification',
                  'remarks',
                  'filled_by',
                  'filled_by_name',
                  'step','next_checker',
                  'next_checker_name',
                  'is_effective','add_time',
                  *read_only_fields]


    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance, validated_data):
        return super().update(instance, validated_data)


class Clsbb_Common_PdfsSerializer(BaseSerializer):
    company_name = CharField(source='company.name', read_only=True, label='公司')
    filled_by_name = CharField(source='filled_by.name',  read_only=True, label='填报人')
    class Meta:
        model = Clsbb_Common_Pdfs
        read_only_fields = ['id', 'company','company_name','name','type',
                            'filled_by','filled_by_name','add_time']
        fields = ['file', *read_only_fields]

    def create(self, validated_data):
        validated_data['name'] = validated_data['file'].name
        validated_data['company'] = self.user.company
        validated_data['type'] = self.request.data['type']
        validated_data['filled_by'] = self.user
        return super().create(validated_data)


class Pam_supplier_management_RecordsSerializer(BaseSerializer):
    create_by_name = CharField(source='create_by.name',  read_only=True, label='填报人')
    update_by_name = CharField(source='create_by.name',  read_only=True, label='填报人')
    class Meta:
        model = Pam_supplier_management_Records
        read_only_fields = ['id','create_by_name','update_by_name','create_time',
                            'update_time','create_by','update_by']
        fields = '__all__'


    def create(self, validated_data):
        instance = super().create(validated_data)
        # 生成编号,用模型中的函数
        instance.number = instance._generate_number()
        # 创建人
        instance.create_by = self.user
        instance.save()
        return instance

    def update(self, instance, validated_data):
        instance = super().update(instance, validated_data)
        # 更新人
        instance.update_by = self.user
        instance.save()
        return instance


# __all__ = [
#     'Pam_business_opportunity_RecordsSerializer',
#     'Clsbbfak_Common_PdfsSerializer',
# ]